# coding: utf8
# 库推荐导入顺序：系统库->框架库->用户库
import platform
import hashlib
import os
import django
from django.views.generic.base import TemplateView
from django.shortcuts import render, HttpResponseRedirect
from django.http import JsonResponse
from django.views.decorators.http import require_http_methods
from django.contrib.auth import authenticate, login
from django.urls import reverse
from django.contrib.auth.models import User
from rest_framework.authtoken.models import Token
from appinput.models import App
from serverinput.models import Server
from deploy.models import DeployPool
from .forms import RegisterForm, LoginForm


class IndexView(TemplateView):
    template_name = "automan/index.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['current_page_name'] = "index"
        context['app_count'] = App.objects.count()
        context['server_count'] = Server.objects.count()
        context['deploy_count'] = DeployPool.objects.count()
        context['REMOTE_ADDR'] = self.request.META.get('REMOTE_ADDR')
        context['HTTP_USER_AGENT'] = self.request.META.get('HTTP_USER_AGENT')
        context['HTTP_ACCEPT_LANGUAGE'] = self.request.META.get('HTTP_ACCEPT_LANGUAGE')
        context['platform'] = platform.platform()
        context['python_version'] = platform.python_version()
        context['django_version'] = django.get_version()
        return context


def redirect_login(request):
    login_url = reverse('index')
    return HttpResponseRedirect(request.POST.get('next', login_url) or login_url)


@require_http_methods(["GET", "POST"])  # 限制请求此视图的request只能是GET和POST方法
def user_register(request):
    error = []
    if request.method == "POST":
        form = RegisterForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            username = data["username"]
            email = data["email"]
            password = data["password"]
            password2 = data["password2"]
            if not User.objects.all().filter(username__iexact=username):
                if form.pwd_validate(password, password2):
                    user = User.objects.create_user(username=username,
                                                    email=email,
                                                    password=password)
                    user.save()
                    user = authenticate(username=username, password=password)
                    login(request, user)
                    return redirect_login(request)
                else:
                    error.append("密码不一致，请确认")
            else:
                error.append("已存在相同用户名，请更换用户名")
        else:
            error.append("请确认各个输入框无误")
        return render(request, "accounts/register.html", locals())
    else:
        form = RegisterForm()
        return render(request, "accounts/register.html", locals())


@require_http_methods(["GET", "POST"])
def user_login(request):
    error = []
    if request.method == 'POST':
        form = LoginForm(request.POST)
        vc = request.POST['vc']
        if vc.upper() != request.session['verify_code']:
            error.append('验证码错误')
            return render(request, "accounts/login.html", locals())
        if form.is_valid():
            data = form.cleaned_data
            username = data['username']
            password = data['password']
            user = authenticate(username=username, password=password)
            if user and user.is_active:
                login(request, user)
                return redirect_login(request)
            else:
                error.append('请输入正确的用户名和密码')
                return render(request, "accounts/login.html", locals())
        else:
            return render(request, "accounts/login.html", locals())
    else:
        form = LoginForm()
        return render(request, "accounts/login.html", locals())


def gettoken(request):
    """获取当前用户的token"""
    if request.method == 'GET':
        token_key = dict()
        token_key["username"] = request.user.username
        token_key["token"] = Token.objects.get(user=request.user).key
        return JsonResponse(token_key)


def token(request):
    """更新随机生成的token值"""
    if request.method == "POST":
        token_key = hashlib.sha1(os.urandom(24)).hexdigest()
        Token.objects.filter(user_id=request.user.id).update(key=token_key)
        context_dict = {
            'token_str': token_key
        }
        return render(request, "automan/token.html", context=context_dict)
    else:
        context_dict = {
            'token_str': Token.objects.get(user=request.user).key
        }
        return render(request, "automan/token.html", context=context_dict)
